/**
 * <p>Title: ServiceZMCE51.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:25:16  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.zm.ce.domain.ZMCEFaultPhenomenon;

/**
 * <p>Title: ServiceZMCE51.java</p>
 * <p>Description: 故障现象列表</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:25:16  张小飞  Create
 *</pre>
 */
public class ServiceZMCE51 extends ServiceEPBase {

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:41:39 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:41:39 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdToencode", inInfo.getString("fdToencode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEFaultPhenomenon.query", new ZMCEFaultPhenomenon());
		outInfo.addBlock(Equipment.getStandardEquipmentBlock("cmes.ce.equipment", null));
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:41:39 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdCode = this.generateCode(inInfo.getBlock("result").getCellStr(0, "fdToencode"));
		if("$".equals(fdCode)){
			inInfo.setStatus(-1);
			inInfo.setMsg("编号的最后四位尾码已经超出了编号规则所规定的四位，请调整已有的数据！");
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdCode",fdCode);
		inInfo.getBlock("result").setCell(0, "fdKeyId",TablePk.getInstance().getNextValue("T_CE_FAULTPHENOMENON"));
		EiInfo outInfo = super.insert(inInfo, "ZMCEFaultPhenomenon.insert");
		return outInfo;
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:41:39 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCEFaultPhenomenon.update");
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE51</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:41:39 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEFaultPhenomenon.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: generateCode</p>
	 * <p>Description: 自动生成故障现象编号</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-9上午10:22:43 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String generateCode(String toencode){
		String code = "";
		List<Map> _list = dao.query("ZMCEFaultPhenomenon.queryLatestCode", null);
		Object fdCode = _list.get(0).get("fdCode");
		if(fdCode == null){
			code = "XX" + toencode + "0001";
		}else{
			String added = (Integer.parseInt(fdCode.toString())+1) + "";
			if(added.length() == 1){
				code = "XX" + toencode + "000" + added;
			}else if(added.length() == 2){
				code = "XX" + toencode + "00" + added;
			}else if(added.length() == 3){
				code = "XX" + toencode + "0" + added;
			}else if(added.length() == 4){
				code = "XX" + toencode + added;
			}else{
				code = "$";//此处设置的“$”为一个固定标识,切勿随意修改
			}
		}
		return code;
	}
}
